package com.javen.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javen.model.ChoiceQuestion;
import com.javen.model.FillingQuestion;
import com.javen.service.IChoiceQuestionService;
import com.javen.service.IFillingQuestionService;



@Controller
@RequestMapping("/question")
/// question/**
public class QuestionController {

	private static Logger log = LoggerFactory.getLogger(QuestionController.class);
	
	@Resource
	private IFillingQuestionService fillingQuestionService;

	@Resource
	private IChoiceQuestionService choiceQuestionService;
	
	
	
	// /question/test?id=1
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test(HttpServletRequest request, Model model) {
		String subject = request.getParameter("subject");
		
		List<FillingQuestion>  fillingQuestionList = new ArrayList<FillingQuestion>();
		fillingQuestionList = fillingQuestionService.getFillingQuestionById(subject);
		int i= 1;
		for(FillingQuestion fillingQuestion :  fillingQuestionList){
			System.out.println(i );
			 ++i;
		
			 
			System.out.println("填空习题:" + '\n' + fillingQuestion.getAnswer() + '\n' + fillingQuestion.getContent() + '\n' + 
						fillingQuestion.getContributor() + '\n' + fillingQuestion.getSubject() );
			 
				log.debug(fillingQuestion.toString());
		 }

		
		model.addAttribute("fillingQuestionList", fillingQuestionList);
		return "index";
		
	}
	
	
	
	// /question/engineerTest
	@RequestMapping(value = "/engineerTest", method = RequestMethod.GET)
	public String engineerTest(HttpServletRequest request, Model model) {
		String subject = request.getParameter("subject");
		
		List<FillingQuestion>  fillingQuestionList = new ArrayList<FillingQuestion>();
		fillingQuestionList = fillingQuestionService.getFillingQuestionById(subject);
		List<ChoiceQuestion>  choiceQuestionList = new ArrayList<ChoiceQuestion>();
		choiceQuestionList = choiceQuestionService.getChoiceQuestionById(subject);
		
		
		int i= 1;
		for(FillingQuestion fillingQuestion :  fillingQuestionList){
			fillingQuestion.setExam_question_id(fillingQuestion.getId()); ;
			fillingQuestion.setId(i);
			
			++i;
			
			
		 }
		for(ChoiceQuestion choiceQuestion :  choiceQuestionList){
			choiceQuestion.setExam_question_id(choiceQuestion.getId()); ;
			choiceQuestion.setId(i);
			 ++i;
		 }

		model.addAttribute("fillingQuestionList", fillingQuestionList);
		model.addAttribute("choiceQuestionList", choiceQuestionList);
		return "engineerTest";
		
	}
	// /user/showUser?id=1
/*	@RequestMapping(value = "/showUser", method = RequestMethod.GET)
	public String toIndex(HttpServletRequest request, Model model) {
		int userId = Integer.parseInt(request.getParameter("id"));
		System.out.println("userId:" + userId);
		User user = this.userService.getUserById(userId);
		log.debug(user.toString());
		model.addAttribute("user", user);
		return "showUser";
	}*/

}
